package jdk8.optional;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class OptionalOperate {

	public static void main(String[] args) {
		
		printConditionString("hello,abc");
		printConditionString("hello,zyx");
		
	}
	
	/****
	 * 判断给定的字符串中是否存在子串:abc，如果存在，则打印出来
	 * @param str
	 */
	public static void printConditionString(String str) {
		//
		Optional.ofNullable(str)  //得到 Optional<String>
				.filter( (t) -> t.contains("abc")) //如果包含abc，就返回this,也就是 Optional<String>
				.ifPresent(System.out::println);
	}
	
	public static Integer getRealAge(Student s) {
		return
			Optional.ofNullable(s)
				.map( (stu) -> stu.getAge())
				//如果这个学员的年龄为null，则返回18
				.orElse(18);
	}
	
	public static Optional<Integer> getAge(Student s) {
		return
				Optional.ofNullable(s)
					.flatMap( (stu) -> Optional.ofNullable(stu.getAge()));
	}
}
 